package ciyuan.Gui.Component;

import ciyuan.MainClass;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextProperties;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.fml.client.gui.GuiUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class StatusWidget extends Button {

    private static final StringTextComponent stringTextComponent = new StringTextComponent("");
    private static final ResourceLocation BACKGROUND_RESOURCE = new ResourceLocation(MainClass.MODID, "textures/gui/player_info/status_card.png");

    private final ResourceLocation iconResource;
    private final Function<ClientPlayerEntity, String> labelText;
    private final List<ITextProperties> tooltips = new ArrayList<>();

    public StatusWidget(int left, int top, Function<ClientPlayerEntity, String> labelText, String name) {
        super(left, top, 38, 20, stringTextComponent, (button) -> {
        });
        this.labelText = labelText;
        this.iconResource = new ResourceLocation(MainClass.MODID, "textures/gui/player_info/" + name + ".png");
        this.tooltips.add(new TranslationTextComponent("ciyuan.gui.playerInfo.label." + name));
    }

    @Override
    public void renderButton(MatrixStack matrixStack, int mouseX, int mouseY, float particleTick) {
        this.renderBg(matrixStack, Minecraft.getInstance(), mouseX, mouseY);
        this.renderIcon(matrixStack, Minecraft.getInstance());

        drawString(matrixStack, Minecraft.getInstance().font, labelText.apply(Minecraft.getInstance().player), this.x + 19, this.y + 6, 16777215);
    }

    @Override
    public void renderToolTip(MatrixStack matrixStack, int mouseX, int mouseY) {
        GuiUtils.drawHoveringText(matrixStack, tooltips, mouseX, mouseY, Minecraft.getInstance().getWindow().getScreenWidth(), Minecraft.getInstance().getWindow().getScreenHeight(), -1, Minecraft.getInstance().font);
    }

    @Override
    protected void renderBg(MatrixStack matrixStack, Minecraft minecraft, int mouseX, int mouseY) {
        minecraft.getTextureManager().bind(BACKGROUND_RESOURCE);
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, this.alpha);
        blit(matrixStack, this.x, this.y, 0, 0, width, height, width, height);
    }

    private void renderIcon(MatrixStack matrixStack, Minecraft minecraft) {
        minecraft.getTextureManager().bind(iconResource);
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, this.alpha);
        blit(matrixStack, this.x + 5, this.y + 5, 0, 0, 10, 10, 10, 10);
    }
}
